'''
    有序链表：无序链表的改进，就是里面的元素就行排序
'''
class Node:     # 节点
    def __init__(self,initdata):    # getData,getNext,setData,setNext
        self.data = initdata
        self.next = None

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self,newdata):
        self.data = newdata

    def setNext(self,newnode):
        self.next = newnode


class OrderedList:  # init，search,add,isEmpty,size,remove
    def __init__(self):
        self.head = None

    def isEmpty(self):
        return self.head == None

    def size(self):
        count = 0
        current = self.head
        while current != None:
            count = count + 1
            current = current.getNext()
        return count

    def search(self,item): # 有序链表
        found = False
        stop = False
        current = self.head
        while current != None and not found and not stop:
            if current.getData() == item:
                found = True
            else:
                if current.getData() > item:
                    stop = True
                else:
                    current = current.getNext()
        return found

    def add(self,item): # 有序链表
        current = self.head
        previous = None
        stop = False
        while current != None and not stop:
            if current.getData() >= item:   # 链表停止条件
                stop = True
            else:   # 继续前进
                previous = current
                current = current.getNext()
        temp = Node(item)
        if previous == None:
            temp.setNext(current)
            self.head = temp    # 头节点要换掉
        else:
            previous.setNext(temp)
            temp.setNext(current)

    def remove(self,item):
        current = self.head
        previous = None
        found = False
        while current != None and not found:
            if current.getData() == item:
                found = True
            else:
                previous = current
                current = current.getNext()
        if previous == None:
            self.head = current.getNext()
        else:
            previous.setNext(current.getNext())

    def printlist(self):
        current = self.head
        while current != None:
            print(current.getData(), end = '')
            print('->',end='')
            current = current.getNext()
        print('None')

if __name__ == '__main__':
    list1 = OrderedList()
    print(list1.isEmpty())
    list1.add(5)
    list1.add(7)
    list1.add(4)
    list1.printlist()
    print(list1.isEmpty())
    print(list1.size())
    print(list1.search(4))
    list1.remove(5)
    list1.printlist()
    print(list1.isEmpty())
